// 通用状态管理工具
const StateManager = {
    // 状态存储
    state: {},
    listeners: {},

    // 设置状态
    setState(key, value) {
        this.state[key] = value;
        this.notifyListeners(key, value);
    },

    // 获取状态
    getState(key) {
        return this.state[key];
    },

    // 订阅状态变化
    subscribe(key, callback) {
        if (!this.listeners[key]) {
            this.listeners[key] = [];
        }
        this.listeners[key].push(callback);
        
        // 返回取消订阅函数
        return () => {
            this.listeners[key] = this.listeners[key].filter(cb => cb !== callback);
        };
    },

    // 通知监听器
    notifyListeners(key, value) {
        if (this.listeners[key]) {
            this.listeners[key].forEach(callback => callback(value));
        }
    },

    // 清除状态
    clearState(key) {
        delete this.state[key];
        if (this.listeners[key]) {
            delete this.listeners[key];
        }
    },

    // 批量设置状态
    setMultipleStates(updates) {
        Object.entries(updates).forEach(([key, value]) => {
            this.setState(key, value);
        });
    }
};

// 通用加载状态管理
const LoadingManager = {
    loadingStates: {},

    setLoading(key, isLoading) {
        this.loadingStates[key] = isLoading;
        StateManager.setState(`loading_${key}`, isLoading);
    },

    isLoading(key) {
        return this.loadingStates[key] || false;
    },

    clearLoading(key) {
        delete this.loadingStates[key];
        StateManager.clearState(`loading_${key}`);
    }
};

// 通用错误状态管理
const ErrorManager = {
    errors: {},

    setError(key, error) {
        this.errors[key] = error;
        StateManager.setState(`error_${key}`, error);
    },

    getError(key) {
        return this.errors[key];
    },

    clearError(key) {
        delete this.errors[key];
        StateManager.clearState(`error_${key}`);
    },

    clearAllErrors() {
        this.errors = {};
        Object.keys(StateManager.state).forEach(key => {
            if (key.startsWith('error_')) {
                StateManager.clearState(key);
            }
        });
    }
};

// 通用通知管理
const NotificationManager = {
    notifications: [],

    addNotification(notification) {
        const id = Date.now() + Math.random();
        const newNotification = {
            id,
            ...notification,
            timestamp: new Date()
        };
        
        this.notifications.push(newNotification);
        StateManager.setState('notifications', [...this.notifications]);
        
        // 自动移除通知
        if (notification.autoClose !== false) {
            setTimeout(() => {
                this.removeNotification(id);
            }, notification.duration || 5000);
        }
        
        return id;
    },

    removeNotification(id) {
        this.notifications = this.notifications.filter(n => n.id !== id);
        StateManager.setState('notifications', [...this.notifications]);
    },

    clearAllNotifications() {
        this.notifications = [];
        StateManager.setState('notifications', []);
    },

    // 便捷方法
    success(message, options = {}) {
        return this.addNotification({
            type: 'success',
            message,
            ...options
        });
    },

    error(message, options = {}) {
        return this.addNotification({
            type: 'error',
            message,
            ...options
        });
    },

    info(message, options = {}) {
        return this.addNotification({
            type: 'info',
            message,
            ...options
        });
    },

    warning(message, options = {}) {
        return this.addNotification({
            type: 'warning',
            message,
            ...options
        });
    }
};

// 注册到全局作用域
window.StateManager = StateManager;
window.LoadingManager = LoadingManager;
window.ErrorManager = ErrorManager;
window.NotificationManager = NotificationManager;
